package com.minglead.springboot.platform.sys.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.minglead.springboot.common.crud.TreeService;
import com.minglead.springboot.common.persistence.Page;
import com.minglead.springboot.platform.sys.dao.OfficeDao;
import com.minglead.springboot.platform.sys.entity.Office;
import com.minglead.springboot.platform.sys.entity.User;
import com.minglead.springboot.platform.sys.utils.UserUtils;



/**
 * 机构Service
 */
@Service
@Transactional(readOnly = true)
public class OfficeService extends TreeService<OfficeDao, Office> {
	@Autowired
	private OfficeDao officeDao;
	@Autowired
	private AreaService areaService;

	public List<Office> findAll(){
		return UserUtils.getOfficeList();
	}

	public List<Office> findList(Boolean isAll){
		if (isAll != null && isAll){
			return UserUtils.getOfficeAllList();
		}else{
			return UserUtils.getOfficeList();
		}
	}
	
	@Transactional(readOnly = true)
	public Page<Office> findList(Page<Office> page,Office office){
		office.setPage(page);
		if(StringUtils.isEmpty(office.getId())){
			office.setId("1");
		}
		page.setList(dao.findByParentIdsLike(office));
		return  page;
	}
	
	public Page<Office> findPage(Page<Office> page, Office office) {
		User u = UserUtils.getUser();
		office.getSqlMap().put("dsf", dataScopeFilter(u, "o", "a"));
		List<Office> list=findList(office);
		page.setList(list);
		return page;
	}
	@Transactional(readOnly = false)
	public void save(Office office) {
		office.setSort(0);
		super.save(office);
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}
	@Transactional(readOnly = false)
	public void delete(Office office) {
		super.delete(office);
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}

	public List<Office> findAllList(Office office) {
		return dao.findAllList(office);
	}
	
	public Office getRoot(){
		return dao.getRoot();
	}
	
}
